package com.fengniao.goods.controller;

import com.fengniao.goods.pojo.Para;
import com.fengniao.goods.service.ParaService;
import com.github.pagehelper.PageInfo;
import entity.DBOperUtil;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lanhai
 * @date 2021/7/5 16:41
 */
@RestController
@RequestMapping("/para")
@CrossOrigin
public class ParaController {

    @Autowired
    private ParaService paraService;

    @GetMapping("/category/{categoryId}")
    public Result<List<Para>> findByCategoryId(@PathVariable Integer categoryId) {
        List<Para> paras = paraService.findByCategoryId(categoryId);
        return Result.data(paras);
    }

    @GetMapping
    public Result<List<Para>> findAll() {
        List<Para> all = paraService.findAll();
        return Result.data(all);
    }

    @GetMapping("/{id}")
    public Result<Para> findById(@PathVariable Integer id) {

        return Result.data(paraService.findById(id));
    }

    @PostMapping
    public Result add(@RequestBody Para para) {

        int flag = paraService.add(para);
        DBOperUtil.checkDBOper(flag, "添加参数失败");
        return Result.ok("添加参数成功");
    }

    @PutMapping("/{id}")
    public Result revise(@PathVariable Integer id, @RequestBody Para para) {

        para.setId(id);
        int flag = paraService.revise(para);
        DBOperUtil.checkDBOper(flag, "添加参数失败");
        return Result.ok("修改参数成功");
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id) {

        int flag = paraService.remove(id);
        DBOperUtil.checkDBOper(flag, "删除参数失败");
        return Result.ok("删除参数成功");
    }

    @PostMapping("/search")
    public Result<List<Para>> findList(@RequestBody Para para) {

        List<Para> list = paraService.findList(para);
        return Result.data(list);
    }

    @GetMapping("/search/{num}/{size}")
    public Result<PageInfo<Para>> findPage(@PathVariable("num") Integer num,
                                           @PathVariable("size") Integer size) {

        PageInfo<Para> page = paraService.findPage(num, size);
        return Result.data(page);
    }

    @PostMapping("/search/{num}/{size}")
    public Result<PageInfo<Para>> findPage(@RequestBody(required = false) Para para,
                                           @PathVariable("num") Integer num,
                                           @PathVariable("size") Integer size) {

        PageInfo<Para> page = paraService.findPage(para, num, size);
        return Result.data(page);
    }
}
